package mutations;

import java.util.ArrayList;
import java.util.List;

import neural.Individual;
import core.Problem;

public class Clasical implements Mutation{

	private double probability;
	//private static double muchomenorqueuno = 0.05;

	public Clasical(double probability) {
		this.probability = probability;
		/*
		 * lo de abajo se pone en otro lado:
		 */
	/*	double rand = Math.random();
		while (rand > muchomenorqueuno) {
			rand = rand * Math.random();
			if (rand == 0) {
				rand = Math.random();
			}
		}
		this.probability = rand;*/
	}

	/**
	 * This method is: if a random number is less than a probability -> we
	 * change that bit
	 * 
	 * @param individual
	 * @return
	 */
	public Individual conventional(Individual individual) {
		//int theBit;
		for (int i = 0; i < individual.getWeights().size(); i++) {
			if (Math.random() < probability) {
				//
				int sign = - 1;
				if(Math.random() < 0.5)
					sign = 1;
				individual.getWeights().set(i, sign * Math.random() * 1.5);
			}
		}
		return individual;
	}
	
	public List<Individual> conventionalAll(List<Individual> childs){
		List<Individual> result = new ArrayList<Individual>();
		for(Individual value:  childs){
			result.add(conventional(value));
		}
		return result;
	}

	@Override
	public List<Individual> mutate(List<Individual> childs, Problem problem) {
		return conventionalAll(childs);
	}

/*
	public static void main(String[] args) {

		Mutations m = new Mutations(0x23FA);
		System.out.println(IndividualConverter.toBinaryString(0x23FA));
		System.out
				.println(IndividualConverter.toBinaryString(m.conventional(0x23FA)));
	}
	*/
}
